package com.sduept.nwld.dataserver.manager.message.interfaces.imp;

import com.sduept.core.entity.SystemParameter;
import com.sduept.message.entity.AlarmMessage;
import com.sduept.message.entity.AlarmMessageWrapper;
import com.sduept.message.entity.MessageType;
import com.sduept.message.interfaces.MessageSendInterface;
import com.sduept.message.manager.MessageReceiveConfigManager;
import com.sduept.message.manager.MessageTypeManager;
import com.sduept.message.observer.MessageSendObserver;
import com.sduept.nwld.dataserver.manager.message.MosMessagePushManager;
import com.sduept.nwld.dataserver.manager.protection.AlarmMessageReceiverManager;
import com.sduept.nwld.dataserver.manager.protection.AlarmReceiveRoleManager;
import com.sduept.permission.entity.PersonnelEntityBean;
import com.sduept.permission.manager.PersonnelManager;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.*;

/** Mos短信消息
 * @version    V1.0
 * @Date       2018年9月21日 上午9:59:08
 */

@Service
public class MosMessageObserver implements MessageSendInterface {

	public static final String MOS_RECEIVER_MESSAGE = "mos_message";

	@Autowired
	private MessageSendObserver messageSendObserver;
	@Autowired
	private MessageReceiveConfigManager messageReceiveConfigManager;
	@Autowired
	private PersonnelManager personnelManager;
	@Autowired
	private MosMessagePushManager mosMessagePushManager;
	@Autowired
	private MessageTypeManager messageTypeManager;
	@Autowired
	private AlarmReceiveRoleManager alarmReceiveRoleManager;
	@Autowired
	private AlarmMessageReceiverManager alarmMessageReceiverManager;

	@PostConstruct
	public void init() {
		register();//实现类监听注册
	}

	/**
	 * 告警消息短信发送：1、先判断消息类型是否使用该发送器。2、再获取消息类型对应的角色人员。3、再判断个人配置。
	 */
	@Override
	public void alarmMessageSend(AlarmMessageWrapper alarmMessageWrapper) {
		AlarmMessage alarmMessage = alarmMessageWrapper.getAlarmMessage();//告警消息
		List<SystemParameter> msgTypes = alarmReceiveRoleManager.initAlarmParams();//所有消息类型
		List<String> typeList = new ArrayList<String>();
		for(SystemParameter sys : msgTypes) {
			typeList.add(sys.getId());
		}
		if(!typeList.contains(alarmMessage.getMsgType())) {//所发送消息类型不包含在告警短信设置的消息类型中
			return;
		}
		Set<String> givenCodes = alarmMessageWrapper.getCodes();//指定人员
		Set<String> phoneList = new HashSet<String>();//短信发送号码
		//根据消息类型，查询接收此消息类型的角色下的人员信息
		Map<String, PersonnelEntityBean> msgReceivers = alarmMessageReceiverManager.findAllMsgReceiver(alarmMessage.getMsgType(), alarmMessage.getStationId());
		if(msgReceivers != null) {
			for (PersonnelEntityBean per : msgReceivers.values()) {
				//查询是否配置此消息接收方式
				boolean flag = messageReceiveConfigManager.exist(per.getCode(), alarmMessage.getMsgType(), MosMessageObserver.MOS_RECEIVER_MESSAGE);
				if(flag) {
					if(StringUtils.isNotEmpty(per.getTelephone())) {
						phoneList.add(per.getTelephone());
					}
				}
			}
		}
		if(null != givenCodes && givenCodes.size() > 0 && alarmMessageWrapper.isForce()) {//指定人员且强制发送
			String[] givenCodesArray = givenCodes.toArray(new String[givenCodes.size()]);
			Set<String> phones = findPhonesByCodes(givenCodesArray);//根据codes查询人员的手机号
			phoneList.addAll(phones);
		}
		MessageType type = messageTypeManager.findTypeById(alarmMessage.getMsgType());
		String title="";
		if(type!=null){
			title=type.getDescription();
		}
		try {
			List<String> phoneLists = new ArrayList<String>();
			phoneLists.addAll(phoneList);
			mosMessagePushManager.sendMsgByMass(phoneLists, title, alarmMessage.getContent());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 根据codes查询人员的手机号
	 * @param codesArr codes数组
	 * @return 人员手机号
	 */
	private Set<String> findPhonesByCodes(String[] codesArr) {
		Set<String> phonesList = new HashSet<String>();
		List<PersonnelEntityBean> personList = personnelManager.findPersonnelByCodes(codesArr);//根据codes查询人员信息
		for (PersonnelEntityBean person : personList) {
			if (StringUtils.isEmpty(person.getTelephone())) {
				continue;
			}
			phonesList.add(person.getTelephone());
		}
		return phonesList;
	}

	@Override
	public void register() {
		messageSendObserver.register(this);
	}

}
